package aes.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

import ocsf.client.ObservableClient;

public class ConnectorUI extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5279744664650935171L;

	private ClientUI ui;
	private Client c;
	
	JLabel lblTitle;
	JButton btnReconnect;

	/**
	 * Constructor
	 * @param ui reference of parent frame
	 */ 
	public ConnectorUI(Client c, ClientUI ui) {
		this.ui = ui;
		this.c = c;
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.add(this);
		
		c.addObserver(this);

		setLayout(new GridBagLayout());

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowWeights = new double[]{0.0, 1.0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);

		lblTitle = new JLabel("Connecting...");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(7, 10, 70, 17);
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 14));

		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.fill = GridBagConstraints.BOTH;
		gbc_lblTitle.insets = new Insets(5, 0, 5, 5);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);
		
		btnReconnect = new JButton("Reconnect", new ImageIcon(ConnectorUI.class.getResource("/aes/client/images/Refresh.png")));
		btnReconnect.addActionListener(this); 
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.insets = new Insets(5, 5, 5, 5);
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 3;
		panel.add(btnReconnect, gbc_btnBack);
		btnReconnect.setBounds(353, 7, 57, 23);
	}

	@SuppressWarnings("static-access")
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof String && ((String)arg).equals(ObservableClient.CONNECTION_ESTABLISHED)) {
			// Connected.
			ui.SetContent(ui.LOGIN_SCREEN);
		}
	}
/**
 * Connect to server
 */
	public void Connect() {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
		      public void run() {
		    	// Connect to server
		  		try {
		  			btnReconnect.setEnabled(false);
		  			c.openConnection();
		  		} catch (IOException e) {
		  			// Can't connect.
		  			e.printStackTrace();
		  			lblTitle.setText("Can't connect to the server.");
		  			btnReconnect.setEnabled(true);
		  		} 
		      }
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnReconnect)) {
			// Back.
			lblTitle.setText("Connecting...");
			btnReconnect.setEnabled(false);
			Connect();
		}
	}
}
